package com.bah.tract.action;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.security.Credentials;
import org.jboss.seam.security.Identity;

import com.bah.tract.model.Person;
import com.bah.tract.model.Project;

@Stateless
@Name("projectFinder")
public class ProjectFinderBean implements ProjectFinder {
	@PersistenceContext
	private EntityManager em;

	@In
	private Credentials credentials;

	@In
	private Identity identity;

	@SuppressWarnings("unused")
	@Out
	private List<Project> reportableProjects;

	@SuppressWarnings("unchecked")
	@Factory("reportableProjects")
	public void getMyProjects() {
		if (identity.hasRole("auditor")) {
			Query query = em.createQuery("select p from Project p");
			reportableProjects = query.getResultList();
		} else {
			Query query = em
					.createQuery("select p from Person p where p.boozAllenID=?1");
			query.setParameter(1, Integer.parseInt(credentials.getUsername()));
			reportableProjects = ((Person) query.getSingleResult())
					.getProjects();
		}
	}
}
